package pl.edu.mimuw.irs.rcp.view.navigation;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import pl.edu.mimuw.irs.rcp.persistence.IExternalResource;
import pl.edu.mimuw.irs.rcp.util.Icon;

public class NavigationViewLabelProvider extends LabelProvider {
	
	public String getText(Object obj) {
		if (obj instanceof IResource) {
			IResource resource = (IResource) obj;
			int dotIndex = resource.getName().lastIndexOf(".");
			String name = resource.getName();
			if (dotIndex > 0) name = name.substring(0, dotIndex);
			return name;
		} else if (obj instanceof IExternalResource) {
			return ((IExternalResource) obj).getName();
		}
		return obj.toString();
	}
	
	public Image getImage(Object obj) {
		String imageKey = ISharedImages.IMG_OBJ_FILE;
		if (obj instanceof IProject) {
			imageKey = ISharedImages.IMG_OBJ_FOLDER;
		} else if (obj instanceof IResource) {
			IResource resource = (IResource) obj;
			if ("irs".equalsIgnoreCase(resource.getFileExtension())) {
				return Icon.WORKFLOW.getImage();
			} else if ("res".equalsIgnoreCase(resource.getFileExtension())) {
				return Icon.PEOPLE.getImage();
			} else if ("bkt".equalsIgnoreCase(resource.getFileExtension())) {
				return Icon.BUCKET.getImage();
			}
		}
		return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
	}
}